<?php
/**
* Yapp Insert wrapper
*/

class Insert extends operationsBase
{

    private $fieldsAndValues = array();

    public function into($tableName)
    {
        return $this->_from($tableName);
    }

    public function values($values = array())
    {
        if ($this->killChaining) return $this;
        else
        {
            if ((!empty($values)) && (!empty($this->tableName)))
            {
                foreach ($values as $fieldName => $fieldValue)
                {
                    $field = $this->getTableAndField($fieldName);
                    if (empty($field['table']))
                    {
                        $table = $this->tableName;
                    }
                    if ($table == $this->tableName)
                    {
                        $this->validateFieldNameAndValue($fieldName, $fieldValue);
                        if (($fieldName !== false) && ($fieldValue !== false))
                        {
                            $this->fieldsAndValues[$fieldName] = $fieldValue;
                        }
                    }
                }
            }
            else
            {
                $this->killChaining = true;
                $this->lastError    = 'No values to insert';
            }
            return $this;
        }
    }

    public function execute($expectingInsertId = false)
    {
        if ($this->killChaining) return false;
        else
        {
            if (!empty($this->fieldsAndValues))
            {
                $sql = $this->buildSql();
                if ($this->dbObject)
                {
                    $this->dbObject->rawQuery($sql);
                    $affectedRows = $this->dbObject->affectedRows();
                    if ($affectedRows)
                    {
                        if ($expectingInsertId)
                        {
                            return $this->dbObject->insertId();
                        }
                        else
                        {
                            return true;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return $sql;
                }
            }
            else
            {
                $this->killChaining = true;
                $this->lastError    = 'No values to insert';
            }
        }
    }

    private function buildSql()
    {
        $sql = 'INSERT INTO '.$this->tableName;
        $fieldList = array();
        $valsList  = array();
        foreach ($this->fieldsAndValues as $field => $value)
        {
            $fieldList[]=$field;
            $valsList[]=$value;
        }
        $sql.= ' ('.implode(',', $fieldList).')';
        $sql.= ' VALUES ';
        $sql.= ' ('.implode(',', $valsList).')';
        return $sql.';';
    }

}
?>